unit BaseFrm;

interface

uses
  Windows, Classes, Messages,
  Controls, StdCtrls, ExtCtrls, ActnList, StdActns,
  DB, DBClient, alBaseDataSet, MConnect,
  Forms, Menus, ImgList,
  PoslovanjeSrv_TLB,
  cxEdit, cxEditRepositoryItems, cxContainer, cxDBEditRepository,
  cxClasses, cxStyles, cxGridBandedTableView;

const
  WS_EX_LAYERED = $80000;
  LWA_COLORKEY = 1;
  LWA_ALPHA = 2;

type
  TfrmBase = class(TForm)
    actnlstMain: TActionList;
    actnOK: TAction;
    actnCancel: TWindowClose;
    styctrlMain: TcxEditStyleController;
    rpstMain: TcxEditRepository;
    imglstMain: TImageList;
    styrpstMain: TcxStyleRepository;
    cxstyContentEven: TcxStyle;
    cxstyContentWarning: TcxStyle;
    cxstyContentPreview: TcxStyle;
    grdstyshtMain: TcxGridBandedTableViewStyleSheet;
    cxstyHeader: TcxStyle;
    cxstyContentSelection: TcxStyle;
    cxstyBackground: TcxStyle;
    procedure actnOKExecute(Sender: TObject);
    procedure actnCancelExecute(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
  private
    FMenu: TMainMenu;
    FSession: ISession;
    function GetMainForm: TForm;
    { Private declarations }
  protected
    procedure Deinitialize; virtual;
    procedure Initialize; virtual;
    procedure SetDefaultControl; virtual;
  public
    constructor Create(AOwner: TComponent); overload; override;
    constructor Create(AOwner: TComponent; ASession: ISession); reintroduce; overload;
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
    property MainForm: TForm read GetMainForm;
    property Menu: TMainMenu read FMenu;
    property Session: ISession read FSession write FSession;
  end;

  TfrmBaseClass = class of TfrmBase;

implementation

uses
  MainFrm;

{$R *.dfm}

procedure TfrmBase.actnCancelExecute(Sender: TObject);
begin
  Close;
  ModalResult := mrCancel;
end;

procedure TfrmBase.actnOKExecute(Sender: TObject);
begin
  Close;
  ModalResult := mrOK;
end;

procedure TfrmBase.AfterConstruction;
begin
  inherited;
  Screen.Cursor := crHourGlass;
  try
    Application.ProcessMessages;
    Initialize;
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TfrmBase.BeforeDestruction;
begin
  Screen.Cursor := crHourGlass;
  try
    Application.ProcessMessages;
    Deinitialize;
  finally
    Screen.Cursor := crDefault;
  end;
  inherited;
end;

constructor TfrmBase.Create(AOwner: TComponent);
var
  i: Integer;
begin
  inherited;
  if Assigned(MainForm) then
  begin
    //Left := Left - ((TfrmMain(MainForm).nbMain.Width div 2) + 2);
    Top := Top - (TfrmMain(MainForm).cbMain.Height - 2);
  end;
  for i := 0 to rpstMain.Count - 1 do
  begin
    if rpstMain.Items[i] is TcxEditRepositoryLookupComboBoxItem then
    begin
      with TcxEditRepositoryLookupComboBoxItem(rpstMain.Items[i]).Properties do
      begin
        ClearKey := TextToShortCut('Del');
        ImmediatePost := False;
        //ListOptions.GridLines := glNone;
        ListOptions.AnsiSort := True;
        ListOptions.ShowHeader := False;
      end;
    end;
  end;
end;

constructor TfrmBase.Create(AOwner: TComponent; ASession: ISession);
begin
  FSession := ASession;
  Create(AOwner);
end;

procedure TfrmBase.Deinitialize;
begin

end;

procedure TfrmBase.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmBase.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    13: begin
      if ssShift in Shift then
        Perform(WM_NEXTDLGCTL, -1, 0)
      else
        Perform(CM_DialogKey, VK_TAB, 0);
    end;
  end;
end;

procedure TfrmBase.FormShow(Sender: TObject);
begin
  SetDefaultControl;
end;

function TfrmBase.GetMainForm: TForm;
begin
  if Owner is TfrmMain then
    Result := TfrmMain(Owner)
  else
    Result := nil;
end;

procedure TfrmBase.Initialize;

  procedure CreateMenu(AMenu: TMainMenu; AGroupIndex: Integer;
    AMenuCaption: string; AActionCategory: string);
  var
    i: Integer;
    Item: TMenuItem;
    SubItem: TMenuItem;
  begin
    Item := TMenuItem.Create(Self);
    Item.Caption := AMenuCaption;
    Item.GroupIndex := AGroupIndex;
    AMenu.Items.Add(Item);
    with actnlstMain do
    begin
      for i := 0 to ActionCount - 1 do
      begin
        if Actions[i].Category = AActionCategory then
        begin
          SubItem := TMenuItem.Create(Self);
          SubItem.Action := Actions[i];
          Item.Add(SubItem);
        end;
      end;
    end;
  end;

begin
  if FormStyle = fsMDIChild then
  begin
    if not Assigned(FMenu) then
      FMenu := TMainMenu.Create(Self);
    CreateMenu(FMenu, 2, 'Opravila', 'Opravila');
    CreateMenu(FMenu, 3, 'Tiskanje', 'Tiskanje');
  end;
end;

procedure TfrmBase.SetDefaultControl;
begin

end;

end.
